package com.kaili.quartz.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.kaili.common.util.CommonConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

/**
 * Created by renzhikun on 18-9-12.
 */
@Configuration
public class DataSourceConfig {
    /**
     * 数据库驱动
     */
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    /**
     * 数据库连接url
     */
    @Value("${spring.datasource.url}")
    private String url;

    /**
     * 数据库连接用户名
     */
    @Value("${spring.datasource.username}")
    private String username;

    /**
     * 数据库连接密码
     */
    @Value("${spring.datasource.password}")
    private String password;

    /**
     * 初始化连接池大小
     */
    @Value("${spring.datasource.initialSize}")
    private Integer initialSize;

    /**
     * 最大连连接池大小
     */
    @Value("${spring.datasource.maxActive}")
    private Integer maxActive;

    /**
     * 连接池最少空闲连接数
     */
    @Value("${spring.datasource.minIdle}")
    private Integer minIdle;

    /**
     * 连接池最大空闲连接数
     */
    @Value("${spring.datasource.maxIdle}")
    private Integer maxIdle;

    /**
     * 获取连接等待超时的时间
     */
    @Value("${spring.datasource.maxWait}")
    private Integer maxWait;

    /**
     * 打开PSCache
     */
    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    /**
     * 指定每个连接上PSCache的大小
     */
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private Integer maxPoolPreparedStatementPerConnectionSize;

    /**
     *检测连接是否有效SQL
     */
    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    /**
     *检测连接是否有效SQL-超时时间
     */
    @Value("${spring.datasource.validationQueryTimeout}")
    private Integer validationQueryTimeout;

    /**
     *申请连接时执行SQL检测是否有效,配置为true时回降低性能
     */
    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    /**
     *归还连接时执行SQL检测是否有效,配置为true时回降低性能
     */
    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    /**
     *建议配置为true,不影响性能,申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测是否有效
     */
    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    /**
     *配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
     */
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private Integer timeBetweenEvictionRunsMillis;

    /**
     *配置一个连接在池中最小生存的时间，单位是毫秒
     */
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private Integer minEvictableIdleTimeMillis;

    /**
     *超过时间限制是否回收
     */
    @Value("${spring.datasource.removeAbandoned}")
    private boolean removeAbandoned;

    /**
     *超时时间,单为为秒
     */
    @Value("${spring.datasource.removeAbandonedTimeout}")
    private Integer removeAbandonedTimeout;

    /**
     * 关闭abanded连接时输出错误日志
     */
    @Value("${spring.datasource.logAbandoned}")
    private boolean logAbandoned;

    /**
     *通过别名的方式配置扩展插件
     */
    @Value("${spring.datasource.filters}")
    private String filters;

    @Bean
    public DataSource dataSource(){
        DruidDataSource config = new DruidDataSource();
        config.setUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.setDriverClassName(driverClassName);
        config.setInitialSize(initialSize);
        config.setMaxActive(maxActive);
        config.setMinIdle(minIdle);
        config.setMaxIdle(maxIdle);
        config.setMaxWait(maxWait);
        config.setValidationQuery(validationQuery);
        config.setValidationQueryTimeout(validationQueryTimeout);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setTestWhileIdle(testWhileIdle);
        config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        config.setRemoveAbandoned(removeAbandoned);
        config.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        config.setLogAbandoned(logAbandoned);
        config.setConnectionProperties("config.decrypt=true;publickey="+CommonConstant.DRUID_PUBLIC_KEY +";password="+password);
        try {
            config.setPasswordCallbackClassName(CommonConstant.DRUID_PASSWORD_CALLBACK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  config;
    }
}
